package com.zhengshi.formal;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class tengxun3 {
	// 乐视采集系列
	public static void main(String[] args) {
		File fileExist = new File("D:\\腾讯视频采集");
		if (!fileExist.exists()) {// 判断目录是否存在
			fileExist.mkdirs();
		}
		
		
		//*****************唯一需要求改的地方
		String urlYuanSheng = "http://v.qq.com/vplus/62bab23da14232b17d8c1a9c4a92e49d/videos";
		//*****************唯一需要求改的地方
		
		
		
		String urlHou ="";
		if(!urlYuanSheng.contains("videos")){
			urlHou = urlYuanSheng+"/videos";
		}else{
			urlHou = urlYuanSheng;
		}
		final WebDriver driver = new ChromeDriver();
		try {
			// driver.manage().window().maximize();

			// 唯一需要更改的地方---------start
			driver.get(urlHou);// 链接采集
			// 唯一需要更改的地方---------end
			Date now = new Date();// 获取系统当前时间
			SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");// 到日期为止
			String strDate = s.format(now);// 格式化日期转换为字符串
			WebElement v_num = driver
					.findElement(By.xpath("//*[@id='headBgMod']/div/div[1]/div[3]/div/ul/li[3]/span[2]"));
			String v_numElement = v_num.getText();// 总数
			//userInfoNick
			WebElement mingcheng = driver.findElement(By.id("userInfoNick"));
			driver.findElement(By.linkText("最热")).click();
			Thread.sleep(800);
			WebElement aaa = driver.findElements(By.className("figures_list")).get(0);
			List<WebElement> bbb = aaa.findElements(By.tagName("li"));
			int a = 0;// 初始化数目
			for (WebElement aaElement : bbb) {
				WebElement aeElement = aaElement.findElement(By.tagName("a"));
				String url = aeElement.getAttribute("href");
				String urlString = url.split("\\?")[0];
				String title = aaElement.findElements(By.tagName("a")).get(1).getAttribute("title").toString();
				// String line = urlString + "●" + title;
				String line = urlString;
				a += 1;
				System.out.println(a + "_排序_" + v_numElement + "_" + line);
				//method2("D:\\腾讯视频按作者采集\\" + strDate + "_" + mingchengElement + "_" + v_numElement + ".txt",line);
				method2("D:\\腾讯视频按作者采集\\" + strDate + ".txt", line);
			}
			
			for (int i = 2; i < 99999; i++) {
				// page_next disabled
				WebElement zuihou = driver.findElement(By.className("mod_pagenav_count"));
				List<WebElement> zuihouA = zuihou.findElements(By.tagName("a"));
				String clazz = zuihouA.get(zuihouA.size()-1).getAttribute("class");
				WebElement aaaa = driver.findElements(By.className("figures_list")).get(0);
				List<WebElement> bbbb = aaaa.findElements(By.tagName("li"));	
				if(bbbb==null||bbbb.size()<1){
					driver.findElement(By.linkText("下一页")).click();
					if ("page_btn page_num current".equals(clazz)) {
						i=99999;
					}
					continue;
				}
				String href = bbbb.get(0).findElement(By.tagName("a")).getAttribute("href");
				if (!"page_btn page_num current".equals(clazz)) {
					driver.findElement(By.linkText("下一页")).click();
					Thread.sleep(800);
					WebElement aaabbb = driver.findElements(By.className("figures_list")).get(0);
					List<WebElement> aaaabbb = aaabbb.findElements(By.tagName("li"));
					if(aaaabbb==null||aaaabbb.size()<1){
						continue;
					}
					for(int g=0;g<3;g++){
						WebElement aaaaa = driver.findElements(By.className("figures_list")).get(0);
						List<WebElement> bbbbb = aaaaa.findElements(By.tagName("li"));	
						String ahref = bbbbb.get(0).findElement(By.tagName("a")).getAttribute("href");
						if(ahref.equals(href)){
							Thread.sleep(50);
						}
					}
					WebElement aaaFor = driver.findElements(By.className("figures_list")).get(0);
					List<WebElement> bbbFor = aaaFor.findElements(By.tagName("li"));
					if (bbbFor != null && bbbFor.size() > 0) {
						for (WebElement aaElement : bbbFor) {
							WebElement aeElement = aaElement.findElement(By.tagName("a"));
							String url = aeElement.getAttribute("href");
							String title = aaElement.findElements(By.tagName("a")).get(1).getAttribute("title").toString();
							String urlString = url.split("\\?")[0];
							// String line = urlString + "●" + title;
							String line = urlString;
							a += 1;
							System.out.println(a + "_排序_" + v_numElement + "_" + line);
							method2("D:\\腾讯视频按作者采集\\" + strDate + ".txt",line);
						}
					}
				} else {
					break;
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		driver.quit();
	}

	// 方法1：

	public void method1() {
		FileWriter fw = null;
		try {
			// 如果文件存在，则追加内容；如果文件不存在，则创建文件
			File f = new File("E:\\dd.txt");
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println("追加内容");
		pw.flush();
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	// 方法2：

	public static void method2(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	// 方法3：

	public static void method3(String fileName, String content) {
		try {
			// 打开一个随机访问文件流，按读写方式
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			// 文件长度，字节数
			long fileLength = randomFile.length();
			// 将写文件指针移到文件尾。
			randomFile.seek(fileLength);
			randomFile.writeBytes(content + "\r\n");
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
